<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<script src="js/mui.js"></script>
		<link href="css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/header.css" />
		<style>
			html,body{
				margin: 0px;
				padding: 0px;
				height: 100%;
			}
			#scanComponent {
				width: 100%;
				top: 44px;
				bottom: 0px;
				position: absolute;
				text-align: center;
				background: black;
			}
		</style>
	</head>

	<body >

		<header class="mui-bar mui-bar-nav title-bar" style="position: fixed;">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left title-content"></a>
			<h1 class="mui-title title-content"><b id="chatting-nickname">扫一扫</b></h1>
		</header>
		<div id="scanComponent">
			
		</div>
		<script type="application/javascript" src="js/app.js"></script>
		<script type="text/javascript">
			mui.init();
			
			mui.plusReady(function(){
				//var user = app.getGlobalUserInfo();
				setTimeout("startScan()", 500);
			})
			
			var scan = null;
			
			/**
			 * 开始扫描
			 */
			function startScan() {
				var barcodeStyles = {
					background: "",
					frameColor: "#128E12",	// 边框颜色
					scanbarColor: "#0062CC"	// 扫码条颜色
				}
				
				scan = new plus.barcode.Barcode("scanComponent", null, barcodeStyles);
				scan.onmarked = onmarked;
				scan.start();
			}
			
			function onmarked(type, result) {
				// 如果是二维码
				if(type === 0) {
					var str = result.split("candywall_qrcode:");
					if(str.length == 2) {
						var friendUsername = str[1];
						var userinfo = app.getGlobalUserInfo();
						// 将用户名发送到后端
						mui.ajax(app.serverUrl + "/user/searchUserinfo",{
							data:{
								myUserId:userinfo.id,
								friendUsername:friendUsername
							},
							dataType:'json',//服务器返回json格式数据
							type:'post',//HTTP请求类型
							timeout:10000,//超时时间设置为10秒；
							headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},	              
							success:function(data){
								plus.nativeUI.closeWaiting();
								// 服务器返回响应，根据响应结果，分析是否登录成功；
								// 控制台中输入日志
								if(data.status == 200) {
									mui.openWindow({
										url: "searchUserForFriendDetail.html",
										id: "searchUserForFriendDetail.html",
										extras: {
											willBeFriend: data.data
										}
									});
									//console.log(JSON.stringify(data.data));
									// 停止扫码，防止不移开一直扫，费电！！！
									// 暂时直接关闭：bug待修复---扫码后如果跳转到好友页面，好友页再返回扫码页扫码就停止了
									scan.stop();
								} else {
									app.showToast(data.msg, "error");
								}
							},
							error:function(xhr,type,errorThrown){
								//异常处理；
								console.log(type);
							}
						});
					}
				}
				//alert(type + "," + result);
				// 扫描完成后继续扫描
				setTimeout("scan.start()", 1000);
			}
		</script>
	</body>

</html>